%% Pattern matching test.
%% 

%-define(ECT_UPGRADE_MODE,mode1).

-include_lib("ect/include/ect.hrl").

-import_class(pattern1).

-class(pattern2).
-superclass(pattern1).

-version(3).
?FIELDS(3, public, {d}).
?FIELDS(3, public, {e}).


-methods([transform/1,upgrade/2]).

-export([dotest/0]).

transform(This1 = #pattern2{e = E}) when E =:= "y" ->
    This2 = {{This1}}:transform(),  %% call superclass counterpart
    This2#pattern2{e = "s"}.      %% set field value

dotest() ->
    %X = #pattern2{a = "p", b = "a", c = "x", d = "y"},
    % construct version 2 of pattern2
    X = 
        case ?ECT_UPGRADE_MODE of
        mode1 -> {pattern2, {pattern1, 2, 2, "p", "a", "x", 1}, {pattern2, 2, 3, "y"}};
        mode2 -> {pattern2_proxy3, {pattern1, 2, pattern1_proxy2, "p", "a", "x", 1}, {pattern2, 2, pattern2_proxy3, "y"}}
        end,
    X2 = {X}:transform(),  % upgrade_local is called implicitly
    #pattern1{a = A, c = C} = X2,
    #pattern1{b = B} = X2,
    #pattern2{e = E} = X2,
    list_to_atom(A++B++C++E).

%vect_match(_, _, _) -> ok.
%vect_set_version(_) -> ok.
%vect_get_version(_) -> 1.


?FIELDS(1, public, {x, y}).
?FIELDS(2, public, {p}).
?FIELDS(1, public, {z = fsgsggr}).
upgrade(New1, Old) ->
    New2 = 
        case ectv_get_local_version(Old) of
        1 ->
            ectv_match(1, {x = X, y = Y, z = Z}, Old),
            New1#pattern2{d = X, e = Y};
        2 ->
            ectv_match(2, {p = P}, Old),
            New1#pattern2{d = semmi, e = P};
        3 ->
            #pattern2{d = D, e = E} = Old,
            New1#pattern2{d = E, e = E}
        end,
    {{New2}}:upgrade(Old).